local sk__bamen = fk.CreateSkill {

  name = "sk__bamen",

  tags = { Skill.Compulsory, },

}



sk__bamen:addEffect(fk.EventPhaseStart, {
  anim_type = "drawcard",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(sk__bamen.name) and player.phase == Player.Play
  end,
  on_use = function(self, event, target, player, data)
    player:throwAllCards("h")
    if player.dead then return end
    local room = player.room
    local cardDic = {}
    for _, id in ipairs(room.draw_pile) do
      local name = Fk:getCardById(id).trueName
      cardDic[name] = cardDic[name] or {}
      table.insert(cardDic[name], id)
    end
    local toObtain = {}
    while #toObtain < 8 and next(cardDic) ~= nil do
      local dicLength = 0
      for _, ids in pairs(cardDic) do
        dicLength = dicLength + #ids
      end
      local randomIdx = math.random(1, dicLength)
      dicLength = 0
      for cardName, ids in pairs(cardDic) do
        dicLength = dicLength + #ids
        if dicLength >= randomIdx then
          table.insert(toObtain, ids[dicLength - randomIdx + 1])
          cardDic[cardName] = nil
          break
        end
      end
    end
    if #toObtain > 0 then
      room:moveCardTo(toObtain, Card.PlayerHand, player, fk.ReasonDraw, sk__bamen.name, nil, false, player)
    end
    local n = 8 - #toObtain
    if n > 0 and not player.dead then
      local to = room:askForChoosePlayers(player, table.map(room:getOtherPlayers(player), Util.IdMapper), 1, 1,
        "#sk__bamen-damage:::"..n, sk__bamen.name, true)
      if #to > 0 then
        room:damage{
          from = player,
          to = room:getPlayerById(to[1]),
          damage = n,
          damageType = fk.ThunderDamage,
          skillName = sk__bamen.name,
        }
      end
    end
  end,
})

return sk__bamen